package net.wendal.nutzbook;

import org.apache.commons.mail.HtmlEmail;
import org.nutz.dao.Dao;
import org.nutz.dao.util.Daos;
import org.nutz.integration.quartz.NutQuartzCronJobFactory;
import org.nutz.ioc.Ioc;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.Setup;

import net.wendal.nutzbook.bean.User;
import net.wendal.nutzbook.service.UserService;

public class MainSetup implements Setup {

	// 注意是init方法,不是depose方法
	public void init(NutConfig conf) {
		Ioc ioc = conf.getIoc();
		Dao dao = ioc.get(Dao.class);
		// 如果提示没有createTablesInPackage方法,请确认用了最新版的nutz,且老版本的nutz已经删除干净
		Daos.createTablesInPackage(dao, "net.wendal.nutzbook", false);
		//通过Daos.migration实现表结构自动修改
		Daos.migration(dao, User.class, true, false);

//		// 初始化默认根用户
//		if (dao.count(User.class) == 0) {
//			User user = new User();
//			user.setName("admin");
//			user.setPassword("123456");
//			user.setCreateTime(new Date());
//			user.setUpdateTime(new Date());
//			dao.insert(user);
//		}
		
	    // 初始化默认根用户
        if (dao.count(User.class) == 0) {
            UserService us = ioc.get(UserService.class);
            us.add("admin", "123456");
        }

		// 获取NutQuartzCronJobFactory从而触发计划任务的初始化与启动
		ioc.get(NutQuartzCronJobFactory.class);
		
		 /**
		  *  测试发送邮件
		  */
        try {
            HtmlEmail email = ioc.get(HtmlEmail.class);
            email.setSubject("测试NutzBook");
            email.setMsg("This is a test mail ... :-)" + System.currentTimeMillis());
            email.addTo("357252539@qq.com");//请务必改成您自己的邮箱啊!!!
            email.buildMimeMessage();
            email.sendMimeMessage();
        } catch (Exception e) {
            e.printStackTrace();
        }
	}

	public void destroy(NutConfig conf) {
	}

}